<?php

namespace addons\sum\controller\trade;

use addons\sum\controller\Base;
use addons\sum\model\User;
use app\admin\model\sum\Config as ConfigModel;
use app\admin\model\sum\trade\Order as TradeOrderModel;


class Order extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $user = User::info();
        $params = $this->request->param();
        $status= $params['status'] ?? 'all';

        $orders = TradeOrderModel::recharge()->where('user_id', $user->id)->paid()
            ->order('id', 'desc')->paginate($this->request->param('list_rows', 10));
        $orders->each(function ($order){
            $order['order_sn_eight']=substr($order['order_sn'], -8);
        });
        $this->success('获取成功', $orders);
    }
    /**
     * 订单详情
     */
    public function detail()
    {
        $user = User::info();
        $params = $this->request->get();
        $order=TradeOrderModel::recharge()->where('user_id', $user->id)->paid()->where('id',$params['id'])->find();
        $order['order_sn_eight']=substr($order['order_sn'], -8);
        $this->success('获取成功', $order);
    }


    public function recharge()
    {
        $user = User::info();
        $params = $this->request->param();

        $fuel_name=$params['fuel_name']??'';
        $fuel_price=$params['fuel_price']??'';

        // 表单验证
        if(!isset($params['recharge_money'])){
            $this->error('充值金额错误');
        }
        $recharge_money = floatval($params['recharge_money']);

        $config = ConfigModel::where('name','recharge')->column('value', 'name');
        $config = json_decode($config['recharge'], true);
        $quick_amounts = $config['quick_amounts'] ?? [];

        $min = $config['min'] ?? 0;
        if($min>0&&$recharge_money<$min){
            $this->error('起充金额：'.$min);
        }

        if ($recharge_money < 0.01) {
            $this->error('请输入正确的充值金额');
        }
        $rule = ['money' => (string)$recharge_money,'gift'=>0];
        foreach ($quick_amounts as $quick_amount) {
            if (bccomp((string)$recharge_money, (string)$quick_amount['money'], 2) === 0) {
                $rule = $quick_amount;
            }
        }

        // 订单自动关闭用
//        $close_time = Config::getConfigs('shop.order.auto_close');
//        $close_time = $close_time && $close_time > 0 ? $close_time : 0;

        $orderData = [];
        $orderData['type'] = 'recharge';
        $orderData['order_sn'] = get_sn($user->id, 'TO');
        $orderData['user_id'] = $user->id;
        $orderData['status'] = TradeOrderModel::STATUS_UNPAID;
        $orderData['order_amount'] = $recharge_money;
        $orderData['pay_fee'] = $recharge_money-$rule['gift'];
        $orderData['remain_pay_fee'] = $recharge_money-$rule['gift'];
        $orderData['platform'] = request()->header('platform');
        $orderData['remark'] = $params['remark'] ?? null;
        $orderData['createtime'] = time();
        $orderData['updatetime'] = time();

        $ext = [
            'fuel_name'=>$fuel_name,
            'fuel_price'=>$fuel_price,
//            'expired_time' => time() + ($close_time * 60),
            'rule' => $rule
        ];

        $orderData['ext'] = $ext;

        $order = new TradeOrderModel();
        $order->save($orderData);

//        if ($close_time) {
//            // 小于等于0， 不自动关闭订单
//            \think\Queue::later(($close_time * 60), '\addons\shopro\job\trade\OrderAutoOper@autoClose', ['order' => $order], 'shopro');
//        }

        $this->success('订单添加成功', $order);
    }
}
